package com.googlecode.jascii;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.googlecode.jascii.util.RotationUtil;

/**
 * @author Kim Lee
 * 
 */

 
public class SidePageResult extends PageResult {
	protected  boolean printSide = false; 
	private boolean sideRight = true;
	private ArrayList sideArray = new ArrayList();
	public SidePageResult() {
	}

	public SidePageResult(int defaultTrNum, ArrayList trArray,
			ArrayList headerArray, ArrayList footerArray) {
		super(defaultTrNum, trArray, headerArray, footerArray);
	}
	public SidePageResult(int defaultTrNum) {
		super(defaultTrNum);
		
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i <  headerArray.size(); i++) {
			TrResult td = (TrResult) headerArray.get(i);
			sb.append(td);
			sb.append("\n");//去掉\r
		}
		List sideTemp=RotationUtil.rotation(sideArray);
		
		for (int i = 0; i < trArray.size(); i++) {
			TrResult td = (TrResult) trArray.get(i);
			sb.append(td);
			if(printSide)sb.append(sideTemp.get(i));
			sb.append("\n"); //去掉\r
		}
		if(printSide && sideTemp.size()>trArray.size())
		{
			for(int i = trArray.size();i<sideTemp.size();i++)
			{
			    TrResult tr = (TrResult)sideTemp.get(i);
			    //System.out.println(tr.getWidth()+"  "+super.maxwidth);
			    sb.append( new TrResult().append( new TdResult("",super.maxwidth , TdResult.TD_STYLE_LEFT) ).append(new TdResult(tr.toString(),tr.getWidth(),TdResult.TD_STYLE_RIGHT) ).toString() );
			    sb.append('\n');
			}
		}
		for (int i = 0; i < footerArray.size(); i++) {
			TrResult td = (TrResult) footerArray.get(i);
			sb.append(td);
			sb.append("\n"); //去掉\r
		}
		return sb.toString();
	}

	
	
	
    public SidePageResult appendSide(TrResult trResult)
    {
    	if (trResult.getHeight() > 1) {
			for (int i = 1; i < trResult.getHeight() + 1; i++) {
				sideArray.add(trResult.getTrResult(i));
			}
		} else
			sideArray.add(trResult);

		return this;
    	
    }

	public void setRepeatSide(boolean repeatSide) {
		this.printSide = repeatSide;
	}


	public void setSideArray(ArrayList sideArray) {
		this.sideArray = sideArray;
	}
	
	/* 
	 * 
	 * Page 由第一頁開始
	 */
	public SidePageResult getPage(int ipage) throws IndexOutOfBoundsException {
		if (ipage == 0)
			throw new IndexOutOfBoundsException("over page");
		int arraySize = trArray.size();
		int totalPageNum = super.getTotalPageNum(true);
		int endIndex = ((Integer)pageIndex.get(ipage-1)).intValue();
		if(ipage==1){ 
			SidePageResult sidePage =  new SidePageResult(defaultTrNum, new ArrayList(
				                               trArray.subList(0, endIndex)),headerArray,footerArray);
			sidePage.setRepeatSide(true);
			sidePage.setSideArray(sideArray);
			sidePage.maxwidth = this.maxwidth;
			return sidePage;
		}
		else
		{
			int startIndex = ((Integer)pageIndex.get(ipage-2)).intValue();
			SidePageResult sidePage =  new SidePageResult(defaultTrNum, new ArrayList(
                    trArray.subList(startIndex, endIndex)),headerArray,footerArray);
			if(printSide){ 
				sidePage.setRepeatSide(printSide);
				sidePage.setSideArray(sideArray);
				sidePage.maxwidth = this.maxwidth;
			}
			return sidePage;
			
		}
			
		
	}
	public static void main(String[] args) {

		SidePageResult table = new SidePageResult(80);

		//TableResult table = new TableResult();
		for (int i = 1; i <= 9; i++) {
			TrResult tr = new TrResult();
			TdResult td = new TdResult("AS" + i, 6, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("BS" + i, 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("CS" + i, 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("CS" + i, 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("CS" + i, 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			table.append(tr);
		}
		
		{
			TrResult tr = new TrResult();
			TdResult td = new TdResult("AS" , 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("BS" , 6, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("CS" , 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("CS" , 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("CS" , 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			table.append(tr);
		}
		{
			TrResult tr = new TrResult();
			TdResult td = new TdResult("AS" , 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("BS" , 6, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("CS" , 8, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			td = new TdResult("CS" , 4, TdResult.TD_STYLE_MIDDLE);
			tr.append(td);
			table.append(tr);
		}
		//page.append(table); 
		
		table.appendSide(new TrResult().append(new TdResult("第一行直式列印測試直式,印測試直式列印測試,印測試直式列印測試,印測試直式列印測試,印測試直式列印測試,印測試直式列印測試,印測試直式列印測試,印測試直式列印測試",80,TdResult.TD_STYLE_LEFT,true)));
		//table.appendSide(new TrResult().append(new TdResult(" ",20,TdResult.TD_STYLE_LEFT)));
		//table.appendSide(new TrResult().append(new TdResult("  第二行",20,TdResult.TD_STYLE_LEFT,true)));
		StringBuffer sb = new StringBuffer();
		for (int i = 1; i <= table.getTotalPageNum(); i++) {
			System.out.println("PAGE=" + i);
			sb.append(table.getPage(i).toString());
			
		}
		System.out.println(sb);
		
		try {
			FileUtils.writeStringToFile(new File("page.txt"), sb.toString(), "BIG5");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	
}